# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
"""
Parse MOT results and generate a LaTeX table.
"""

MOTS = False
MOT20 = False
# F_CONTENT = """
# 	MOTA	IDF1	MOTP	MT	ML	FP	FN	Recall	Precision	FAF	IDSW	Frag
#     MOT17-01-DPM	41.6	44.2	77.1	5	8	496	3252	49.6	86.6	1.1	22	58
#     MOT17-01-FRCNN	41.0	42.1	77.1	6	9	571	3207	50.3	85.0	1.3	25	61
#     MOT17-01-SDP	41.8	44.3	76.8	7	8	612	3112	51.8	84.5	1.4	27	65
#     MOT17-03-DPM	79.3	71.6	79.1	94	8	1142	20297	80.6	98.7	0.8	191	525
#     MOT17-03-FRCNN	79.6	72.7	79.1	93	7	1234	19945	80.9	98.6	0.8	180	508
#     MOT17-03-SDP	80.0	72.0	79.0	93	8	1223	19530	81.3	98.6	0.8	181	526
#     MOT17-06-DPM	54.8	42.0	79.5	54	63	314	4839	58.9	95.7	0.3	175	244
#     MOT17-06-FRCNN	55.6	42.9	79.3	57	59	363	4676	60.3	95.1	0.3	190	264
#     MOT17-06-SDP	55.5	43.8	79.3	56	61	354	4712	60.0	95.2	0.3	181	262
#     MOT17-07-DPM	44.8	42.0	76.6	11	16	1322	7851	53.5	87.2	2.6	147	275
#     MOT17-07-FRCNN	45.5	41.5	76.6	13	15	1263	7785	53.9	87.8	2.5	156	289
#     MOT17-07-SDP	45.2	42.4	76.6	13	15	1332	7775	54.0	87.3	2.7	147	279
#     MOT17-08-DPM	26.5	32.2	83.0	11	37	378	15066	28.7	94.1	0.6	88	146
#     MOT17-08-FRCNN	26.5	31.9	83.1	11	36	332	15113	28.5	94.8	0.5	89	141
#     MOT17-08-SDP	26.6	32.3	83.1	11	36	350	15067	28.7	94.5	0.6	91	147
#     MOT17-12-DPM	46.1	53.1	82.7	16	45	207	4434	48.8	95.3	0.2	30	50
#     MOT17-12-FRCNN	46.1	52.6	82.6	15	45	197	4443	48.7	95.5	0.2	30	48
#     MOT17-12-SDP	46.0	53.0	82.6	16	45	221	4426	48.9	95.0	0.2	30	52
#     MOT17-14-DPM	31.6	36.6	74.8	13	78	636	11812	36.1	91.3	0.8	196	331
#     MOT17-14-FRCNN	31.6	37.6	74.6	13	77	780	11653	37.0	89.8	1.0	202	350
#     MOT17-14-SDP	31.7	37.1	74.7	13	76	749	11677	36.8	90.1	1.0	205	344
#     OVERALL 61.5	59.6	78.9	621 	752	14076	200672	64.4	96.3	0.8	2583	4965
#     """

F_CONTENT = """
	MOTA	MOTP	IDF1	IDP	IDR	TP	FP	FN	Rcll	Prcn	MTR	PTR	MLR	MT	PT	ML	IDSW	FAR	FM
    MOT17-01-DPM	49.92	79.58	42.97	58.18	34.06	3518	258	2932	54.54	93.17	20.83	45.83	33.33	5	11	8	40	0.57	50
    MOT17-01-FRCNN	50.87	79.26	42.33	55.77	34.11	3637	308	2813	56.39	92.19	33.33	41.67	25.00	8	10	6	48	0.68	57
    MOT17-01-SDP	53.66	78.16	45.33	54.31	38.90	4064	556	2386	63.01	87.97	41.67	37.50	20.83	10	9	5	47	1.24	72
    MOT17-03-DPM	74.05	79.41	66.45	76.34	58.83	79279	1389	25396	75.74	98.28	57.43	30.41	12.16	85	45	18	374	0.93	420
    MOT17-03-FRCNN	75.34	79.45	66.98	76.21	59.75	80635	1434	24040	77.03	98.25	56.76	32.43	10.81	84	48	16	335	0.96	409
    MOT17-03-SDP	79.64	79.04	65.84	72.00	60.65	86043	2134	18632	82.20	97.58	64.19	27.03	8.78	95	40	13	545	1.42	522
    MOT17-06-DPM	53.62	82.55	51.83	64.47	43.33	7209	711	4575	61.18	91.02	28.38	37.84	33.78	63	84	75	180	0.60	170
    MOT17-06-FRCNN	57.21	81.73	54.75	63.67	48.02	7928	960	3856	67.28	89.20	32.88	45.50	21.62	73	101	48	226	0.80	223
    MOT17-06-SDP	56.43	81.93	54.00	62.70	47.42	7895	1017	3889	67.00	88.59	36.94	37.39	25.68	82	83	57	228	0.85	222
    MOT17-07-DPM	52.59	80.54	48.08	66.84	37.54	9230	258	7663	54.64	97.28	20.00	53.33	26.67	12	32	16	88	0.52	148
    MOT17-07-FRCNN	52.39	80.11	47.88	64.56	38.05	9456	499	7437	55.98	94.99	20.00	61.67	18.33	12	37	11	106	1.00	174
    MOT17-07-SDP	54.56	79.84	47.81	62.29	38.79	9928	590	6965	58.77	94.39	26.67	55.00	18.33	16	33	11	121	1.18	199
    MOT17-08-DPM	32.52	83.93	31.85	60.34	21.63	7286	288	13838	34.49	96.20	13.16	44.74	42.11	10	34	32	128	0.46	154
    MOT17-08-FRCNN	31.11	84.47	31.68	62.05	21.27	6958	285	14166	32.94	96.07	13.16	39.47	47.37	10	30	36	102	0.46	120
    MOT17-08-SDP	34.96	83.31	33.05	58.02	23.11	7972	443	13152	37.74	94.74	15.79	48.68	35.53	12	37	27	144	0.71	175
    MOT17-12-DPM	51.26	83.01	57.74	72.70	47.88	5102	606	3565	58.87	89.38	23.08	42.86	34.07	21	39	31	53	0.67	86
    MOT17-12-FRCNN	47.71	83.16	56.73	72.39	46.64	4882	702	3785	56.33	87.43	20.88	43.96	35.16	19	40	32	45	0.78	72
    MOT17-12-SDP	48.88	82.87	57.46	70.30	48.59	5140	850	3527	59.31	85.81	24.18	45.05	30.77	22	41	28	54	0.94	89
    MOT17-14-DPM	38.07	77.47	42.03	66.15	30.80	7978	627	10505	43.16	92.71	9.15	52.44	38.41	15	86	63	314	0.84	296
    MOT17-14-FRCNN	37.78	76.70	41.78	59.55	32.18	8688	1300	9795	47.01	86.98	10.37	55.49	34.15	17	91	56	406	1.73	382
    MOT17-14-SDP	40.40	76.40	42.38	57.96	33.40	9277	1376	9206	50.19	87.08	10.37	59.76	29.88	17	98	49	434	1.83	437
    OVERALL\t62.30\t79.77\t57.58\t70.58\t48.62\t372105\t16591	192123	65.95	95.73	29.21	43.69	27.09	688	1029	638	4018	0.93	4477
    """


# MOTS = True
# F_CONTENT = """
#     sMOTSA	MOTSA	MOTSP	IDF1	MT	ML	MTR	PTR	MLR	GT	TP	FP	FN	Rcll	Prcn	FM	FMR	IDSW	IDSWR
#     MOTS20-01	59.79	79.56	77.60	68.00	10	0	83.33	16.67	0.00	12	2742	255	364	88.28	91.49	37	41.91	16	18.1
#     MOTS20-06	63.91	78.72	82.85	65.14	115	22	60.53	27.89	11.58	190	8479	595	1335	86.40	93.44	218	252.32	158	182.9
#     MOTS20-07	43.17	58.52	76.59	53.60	15	17	25.86	44.83	29.31	58	8445	834	4433	65.58	91.01	177	269.91	75	114.4
#     MOTS20-12	62.04	74.64	84.93	76.83	41	9	60.29	26.47	13.24	68	5408	549	1063	83.57	90.78	76	90.94	29	34.7
#     OVERALL	54.86	69.92	80.62	63.58	181	48	55.18	30.18	14.63	328	25074	2233	7195	77.70	91.82	508	653.77	278	357.8
#     """


MOT20 = True
F_CONTENT = """
	MOTA	MOTP	IDF1	IDP	IDR	HOTA	DetA	AssA	DetRe	DetPr	AssRe	AssPr	LocA	TP	FP	FN	Rcll	Prcn	IDSW\tMT\tML
    MOT20-04	82.72	82.57	75.59	79.81	71.79	63.21	68.29	58.64	73.11	81.27	63.43	80.18	84.53	236919	9639	37165	86.44	96.09	566\t490\t28
    MOT20-06	55.88	79.00	53.51	68.11	44.07	43.85	45.80	42.23	49.13	75.94	45.95	74.07	81.72	80317	5582	52440	60.50	93.50	545\t96\t72
    MOT20-07	56.21	85.22	59.05	78.90	47.18	49.19	48.45	50.21	50.63	84.68	53.31	83.48	86.86	19245	547	13856	58.14	97.24	92\t41\t20
    MOT20-08	46.03	77.71	48.34	65.65	38.26	38.89	38.46	39.70	41.87	71.85	43.36	71.76	81.08	40572	4580	36912	52.36	89.86	329\t39\t61
    OVERALL\t68.64	81.42	65.70	75.63	58.08	54.67	56.68	52.97	60.84	79.22	57.39	78.50	83.69	377053	20348	140373	72.87	94.88	1532\t666\t181
"""


if __name__ == '__main__':
    # remove empty lines at start and beginning of F_CONTENT
    F_CONTENT = F_CONTENT.strip()
    F_CONTENT = F_CONTENT.splitlines()

    start_ixs = range(1, len(F_CONTENT) - 1, 3)
    if MOTS or MOT20:
        start_ixs = range(1, len(F_CONTENT) - 1)

    metrics_res = {}

    for i in range(len(['DPM', 'FRCNN', 'SDP'])):
        for start in start_ixs:
            f_list = F_CONTENT[start + i].strip().split('\t')
            metrics_res[f_list[0]] = f_list[1:]

        if MOTS or MOT20:
            break

    metrics_names = F_CONTENT[0].replace('\n', '').split()

    print(metrics_names)

    metrics_res['ALL'] = F_CONTENT[-1].strip().split('\t')[1:]

    for full_seq_name, data in metrics_res.items():
        seq_name = '-'.join(full_seq_name.split('-')[:2])
        detection_name = full_seq_name.split('-')[-1]

        if MOTS:
            print(f"{seq_name} & "
                f"{float(data[metrics_names.index('sMOTSA')]):.1f} & "
                f"{float(data[metrics_names.index('IDF1')]):.1f} & "
                f"{float(data[metrics_names.index('MOTSA')]):.1f} & "
                f"{data[metrics_names.index('FP')]} & "
                f"{data[metrics_names.index('FN')]} & "
                f"{data[metrics_names.index('IDSW')]} \\\\")
        else:
            print(f"{seq_name} & {detection_name} & "
                f"{float(data[metrics_names.index('MOTA')]):.1f} & "
                f"{float(data[metrics_names.index('IDF1')]):.1f} & "
                f"{data[metrics_names.index('MT')]} & "
                f"{data[metrics_names.index('ML')]} & "
                f"{data[metrics_names.index('FP')]} & "
                f"{data[metrics_names.index('FN')]} & "
                f"{data[metrics_names.index('IDSW')]} \\\\")
